﻿<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/include/taglib.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%@ include file="/include/header.jsp" %>
<!-- 校检规则 -->
<script src="${ctx}/js/lib/jquery-ui.custom.js" type="text/javascript"></script>
<%@ include file="/include/zTree.jsp" %>
</head>
<body style="background-color: white;">
	<div style="padding: 10px;">
	<form id="sysAuthorityForm"  action="post">
	    <input type="hidden" name="CSRFToken" value="${csrfToken}" />
		<input type="hidden" name="authorityId" value="${sysAuthorityShowVo.authorityId}" />
		<input type="hidden" name="authorityValue" value="${sysAuthorityShowVo.authorityValue}" />
		<input type="hidden" name="levelCode" value="${sysAuthorityShowVo.levelCode}" />
		
	   <div class="b_box">
		<div class="p_bar"></div>
		<div class="con_box ">
	        <div class="clearfix">
				<table class="c_table">
					<tr>
						
						<td   width="150"  class="t_r"><b><i class="square"></i>系统名称：</b></td>
						<td  width="170"><input type="text" class="input_txt"  id="authorityName" name="authorityName" value="${sysAuthorityShowVo.authorityName}" data-validaterole="authorityName" data-rule="authorityName" style="width:150px;"/></td>
						<td  width="150" class="t_r"><b><i class="square"></i>状&nbsp;&nbsp;&nbsp;&nbsp;态：</b></td>
						<td><select id="status" name="status" class="input_txt" style="width:150px;">
								<c:forEach items="${statusMap}" var="item">
									<option id="${item.key}" value="${item.key}" <c:if test="${item.key==sysAuthorityShowVo.status}"> selected="selected"</c:if>>${item.value}</option>
								</c:forEach>
							</select>
					     </td>
					</tr>
					<tr>
							<td width="120" class="t_r"><b><i class="square"></i>权&nbsp;限&nbsp;值：</b></td>
						<td>${sysAuthorityShowVo.authorityValue}</td>
						<td class="t_r"><b><i class="square"></i>等&nbsp;&nbsp;&nbsp;&nbsp;级：</b></td>
						<td><input type="text" class="input_txt" id="grade" name="grade" value="${sysAuthorityShowVo.grade}" data-validaterole="grade" data-rule="grade" style="width: 50px"/></td>
					</tr>
					<tr>
						<td class="t_r"><b><i class="square"></i>权限类型：</b></td>
						<td>
							<select id="authorityType" name="authorityType" class="input_txt" style="width:150px;">
								<c:forEach items="${authTypeMap}" var="item">
									<option id="${item.key}" value="${item.key}" <c:if test="${item.key==sysAuthorityShowVo.authorityType }"> selected="selected"</c:if>>${item.value}</option>
								</c:forEach>
							</select>
						</td>
						 <td class="t_r"><b><i class="square"></i>排&nbsp;序&nbsp;号：</b></td>
						<td><input type="text" class="input_txt" id="sortNo" name="sortNo" value="${sysAuthorityShowVo.sortNo}" data-validaterole="sortNo" data-rule="sortNo" style="width: 50px"/></td>
					</tr>
					<tr>
					
					</tr>
				
					<tr>
					     <td class="t_r"><b><i class="square"></i>父级权限：</b></td>
						<td colspan="3">
						        <input type="text" id="parentAuthorityName" readonly="readonly" class="input_txt" value="${sysAuthorityShowVo.parentAuthorityName}"/>
								<a class="menuBtn" href="javascript:showMenu();">选择</a>&nbsp;&nbsp;<a class="menuBtn" href="javascript:clear();">清除</a>
								<input type="hidden" name="parentId" id="parentId" value="${sysAuthorityShowVo.parentId}"/>
						</td>
					</tr>
				
					<tr>
					     <td class="t_r"><b><i class="square"></i>请求地址：</b></td>
						<td colspan="3">
						        <textarea id="action" name="action" class="input_txt" style="height:60px;width: 450px">${sysAuthorityShowVo.action}</textarea>
						</td>
					</tr>
					<tr>
						<td class="t_r"><b><i class="square"></i>注&nbsp;&nbsp;&nbsp;&nbsp;释：</b></td>
						<td colspan="3"><textarea id="remark" name="remark" class="input_txt" style="height:60px;width: 450px">${sysAuthorityShowVo.remark}</textarea></td>
					</tr>
				</table>
			</div>
	      </div>
	 </div>
	 
	</form>
		<div class="action_box m_t10 t_c"><input type="button" class="a_btn" value="保存" id="editBtn"/></div>
	</div>
	<div id="menuContent" class="menuContent" style="display:none; position: absolute;background-color: white;">
		<ul id="treeDemo" class="ztree" style="margin-top:0; width:300px;"></ul>
	</div>
	
	<script type="text/javascript">
		
		function clear() {
			$("#parentAuthorityName").val("");
			$("#parentId").val("");
		}
		function showMenu() {
			var cityObj = $("#parentAuthorityName");
			var cityOffset = $("#parentAuthorityName").offset();
			$("#menuContent").css({left:cityOffset.left + "px", top:cityOffset.top + cityObj.outerHeight() + "px"}).slideDown("fast");

			$("body").bind("mousedown", onBodyDown);
		}
		function hideMenu() {
			$("#menuContent").fadeOut("fast");
			$("body").unbind("mousedown", onBodyDown);
		}
		function onBodyDown(event) {
			if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
				hideMenu();
			}
		}
		
		function initTree() {
			var setting = {
				view: {
					dblClickExpand: false
				},
				data: {
					simpleData: {
						enable: true
					}
				},
				callback: {
					beforeClick: beforeClick,
					onClick: onClick,
					asyncSuccess: zTreeOnAsyncSuccess,//异步加载成功的fun 
					asyncError: zTreeOnAsyncError, //加载错误的fun
				},async: { 
					enable: true, 
					url:"${ctx}/sysAuthority/authorityMenuTree?initId=${sysAuthorityShowVo.authorityId}",
					autoParam:["id"], 
					dataType: "json",//默认text 
					//type:"get",//默认post 
					dataFilter: filter //异步返回后经过Filter 
				}
			};
			
			function onClick(e, treeId, treeNode) {
				var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
				nodes = zTree.getSelectedNodes();
				
				if(nodes.length > 0) {
					var parentAuthorityNameObj = $("#parentAuthorityName");
					parentAuthorityNameObj.attr("value", nodes[0].name);
					var parentIdObj = $("#parentId");
					parentIdObj.attr("value", nodes[0].id);
				}
				hideMenu();
			}
			
			//treeId是treeDemo 
			function filter(treeId, parentNode, childNodes) { 
				if (!childNodes) return null; 
				for (var i = 0, l = childNodes.length; i < l; i++) {
					childNodes[i].name = childNodes[i].name.replace('', '');
				}
				return childNodes;
			}

			function beforeClick(treeId, treeNode) {
				return true;
			}
			function zTreeOnAsyncError(event, treeId, treeNode) {
				alert("异步加载失败!");
			}
			function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) {

			}
			$.fn.zTree.init($("#treeDemo"), setting);
		}

		$(function() {
			initTree();

			$("#parentAuthorityName").click(function() {
				showMenu();
			});
			
			//如果是顶级权限,则不允许修改添加上级权限
			if("${sysAuthorityShowVo.parentId}"==""){
				$(".menuBtn").hide();
				$("#parentAuthorityName").attr("disabled",true);
			}

			// 验证
			seajs.use(["component/validate", "component/tip"],
			function(validate, tip) {
				var ntip = function() {
					// 定义一个提示对象
					var t = new tip({
						content: 'hello tip',
						hasArrow: true,
						arrowOption: {
							dir: "left",
							position: {
								top: 8
							}
						},
						con: "body",
						hasCloseBtn: false,
						position: {
							my: "center",
							at: "center",
							of: "#tipcon"
						},
						css: {
							width: "120"
						}
					});
					return t;
				};

				// 定义一个验证对象
				var d = new validate({
					con: "#sysAuthorityForm",
					// 验证容器
					onSubmit: "#editBtn",
					// 提交按钮
					eachInvalidField: function(res, field) { // 验证不通过的处理
						var oldTip = field.data("tip");
						var tip;
						if (typeof oldTip == "undefined") {
							tip = ntip();
							field.data("tip", tip);
						} else {
							tip = oldTip;
						}
						tip.setContent(res.message);
						tip.setPosition({
							my: "left center-15",
							at: "right+10 center",
							of: field
						});
						tip.show();
					},
					eachValidField: function(res, field) { // 验证通过则隐藏提示
						var oldTip = field.data("tip");
						var tip;
						if (typeof oldTip == "undefined") {

						} else {
							tip = oldTip;
							tip.hide();
						}
					}
				});
				// 设置拼装规则
				d.setValidateRule({authorityName: ["isBlank"]});
				d.setValidateRule({grade: ["isBlank"]});
				d.setValidateRule({sortNo: ["isNum"]});
				// 绑定提交按钮
				d.bind("onsubmit",
				function() {
					
					var status1 = "${sysAuthorityShowVo.status}";
					var status2 = $("#status").val();
					if (status1 != status2) {
						if(status1=="1"){
							if (!confirm('停用该节点的同时会将其下辖节点也一并停用,是否确定停用？')) {
								return;
							}
						}else{
							if (!confirm('启用该节点的同时会将其父节点也一并启用,是否确定启用？')) {
								return;
							}
						}
					}
					
					var serializedForm = $("#sysAuthorityForm").serialize();
					$.post("${ctx}/sysAuthority/edit", serializedForm,
					function(result) {
						if ("succ" == result) {
							alert("操作成功");
							parent.replaceByUrl('${ctx}/sysAuthority/list');
							parent.layerClose(window.name);
			            }else {
							alert(result);
						}
					},
					"json");
				});
			});
		});
	</script>
</body>
</html>